package com.example.a14874.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class StuContentProvider  extends ContentProvider{

    private StuOpenHelper openHelper;
    private static final String AUTOHORITY = "com.gdcp.student";
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        uriMatcher.addURI(AUTOHORITY, "student", 1);
        uriMatcher.addURI(AUTOHORITY, "score", 2);
    }

    @Override
    public boolean onCreate() {
        openHelper = new StuOpenHelper(getContext(),"stu.db",null, 1);
        return true;
    }

    @Nullable
    @Override
    public Cursor query( @NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1 ) {
        SQLiteDatabase sqLiteDatabase = openHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)){
            case 1:
                Cursor cursor1 = sqLiteDatabase.query("student", strings, s,strings1,null, null, s1);
                return cursor1;

            case 2:
                Cursor cursor2 = sqLiteDatabase.query("score",strings, s,strings1,null, null, s1);
                return cursor2;

        }
        return null;
    }

    @Nullable
    @Override
    public String getType( @NonNull Uri uri ) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert( @NonNull Uri uri, @Nullable ContentValues contentValues ) {
        SQLiteDatabase sqLiteDatabase = openHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)){
            case 1:
                sqLiteDatabase.insert("student", null, contentValues);
                break;

            case 2:
                sqLiteDatabase.insert("score", null, contentValues);
                break;
        }

        sqLiteDatabase.close();
        return null;
    }


    @Override
    public int delete( @NonNull Uri uri, @Nullable String s, @Nullable String[] strings ) {
        return 0;
    }

    @Override
    public int update( @NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings ) {
        return 0;
    }
}
